﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using LinkCard.Utils;
using System.Data.SqlClient;
using LinkCard.Dao;

namespace LinkCard.CustomerView
{
    public partial class ArticleList : System.Web.UI.Page
    {
        private int _totalRecords;
        private int nPageSize = 10;
        public int nCategoryID;
        public string sCateNameRewrite;
        string keyword = "";
        protected void Page_Load(object sender, EventArgs e)
        {

            //nCategoryID = ConvertUtility.ToInt32(Page.RouteData.Values["cid"]);
            var tag = Page.RouteData.Values["keyword"];
            nCategoryID = ConvertUtility.ToInt32(Page.RouteData.Values["cid"]);
            if (tag != null)
            {
                keyword = tag.ToString();

                int curpage = 0;
                if (Request.QueryString["page"] != null)
                {
                    curpage = Convert.ToInt32(Request.QueryString["page"]);
                }
                else
                {
                    curpage = 1;
                }
                var s = new MessageDao().GetMessageByTag(keyword, nCategoryID, curpage, nPageSize);
                msgList.DataSource = s;

                int nPageCount;
                _totalRecords = new MessageDao().GetMessageByTagCount(keyword, nCategoryID);
                if (_totalRecords % nPageSize == 0)
                {
                    nPageCount = _totalRecords / nPageSize;
                }
                else
                {
                    nPageCount = Convert.ToInt32(_totalRecords / nPageSize) + 1;
                }

                if (nPageCount < 2) divPager.Visible = false;

                PagerShow.Text = hash1way.ShowPageNavigation(curpage, nPageCount);

                msgList.DataBind();
                msgNoData.Visible = false;
            }
            else
            {
                DataBindPaging(nCategoryID);
            }
            SetMsgCate(nCategoryID);

        }
        private void DataBindPaging(int nCategoryID)
        {

            try
            {
                
                string sFilter = "";
                if (nCategoryID > 0) sFilter = "and MessageCate.CategoryID = " + nCategoryID;

                int curpage = 0;

                if (Request.QueryString["page"] != null)
                {
                    curpage = Convert.ToInt32(Request.QueryString["page"]);
                }
                else
                {
                    curpage = 1;
                }



                DataTable dt = DataHelper.LoadPageInSQL("FontEnd_MessagesList", curpage, nPageSize, ref _totalRecords, new SqlParameter("@Filter", sFilter));

                if (dt.Rows.Count > 0)
                {
                    int nPageCount;

                    if (_totalRecords % nPageSize == 0)
                    {
                        nPageCount = _totalRecords / nPageSize;
                    }
                    else
                    {
                        nPageCount = Convert.ToInt32(_totalRecords / nPageSize) + 1;
                    }

                    if (nPageCount < 2) divPager.Visible = false;

                    PagerShow.Text = hash1way.ShowPageNavigation(curpage, nPageCount);

                    msgList.DataSource = dt;

                    msgList.DataBind();
                    msgNoData.Visible = false;

                }

            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        protected void SetMsgCate(int nCategoryID)
        {
            var dt = DataHelper.SelectTable("Select Name from messageCate where CategoryID = " + nCategoryID,
                                                  false);
            if (dt.Rows.Count <= 0) return;

            msgCate.Text = dt.Rows[0]["Name"].ToString();

            Page.Title = dt.Rows[0]["Name"] + " - LinkCard";
            Page.MetaDescription = Page.Title;
            Page.MetaKeywords = "";
            sCateNameRewrite = Utilities.ConvertTextToUrl(dt.Rows[0]["name"].ToString());

            if (!string.IsNullOrEmpty(keyword))
            {
                ltkeyword.Text = keyword;
            }
            else
            {
                litag.Visible = false;
            }
        }
    }
}